﻿using LPP.Core.Services;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace LPP.API.Controllers.MediaController
{
    [Route("api/[controller]")]
    [ApiController]
    public class MediaController : ControllerBase
    {
        private readonly IMediaService _mediaService;

        public MediaController(IMediaService mediaService)
        {
            _mediaService = mediaService;
        }

        [HttpPost("upload-audio")]
        public async Task<IActionResult> UploadAudio(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest(new { message = "No file provided." });
            }

            // 校验文件是否为音频类型，支持常见的音频格式如 mp3, wav, m4a 等
            if (!file.ContentType.StartsWith("audio/"))
            {
                return BadRequest(new { message = "Invalid file type. Only audio files are allowed." });
            }

            try
            {
                // 调用服务层处理文件上传，传递文件类型为 "audio"
                var media = await _mediaService.UploadMediaAsync(file, "audio");

                return Ok(new { message = "Audio upload successful", media });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { message = "An error occurred during upload.", error = ex.Message });
            }
        }
    }
}
